﻿function PureAjax(){
	var ajaxObject=false;//全局XMLHttpRequest对象
	//创建XMLHttpRequest对象的方法
	var getResult;
	
	function createObject(){
		if(window.XMLHttpRequest){
			ajaxObject=new XMLHttpRequest();//DOM标准
		}else if(window.ActiveXObject){//兼容IE浏览器
			try{
				ajaxObject=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					ajaxObject=new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					ajaxObject=false;
				}
			
			}
		}
	};
	function dealResponseData(dataType,data){
		switch(dataType){
			case "text":
				return data;
				break;
			case "json":
				return eval('object='+data);
				break;
			case "xml":
				break;
			case "html":
				return data;
				break;
			default:
				return data;
		}

	}
	createObject();//自动初始化XMLHttpRequest对象
	//处理服务器返回的
	this.dealJSinHTML=function(html,dom){
		var obj=document.getElementById(dom);
		obj.innerHTML=html;
		for(var i=0;i<obj.childNodes.length;i++){
			if(obj.childNodes[i].nodeName.toLowerCase()=="script"){
				eval(obj.childNodes[i].innerHTML);
			}
		}
	}
	//GET方式提交数据
	this.get=function(param){
		var dataType="text";//数据传输格式,默认为text
		var data=this.objToStr(param.data);
		var async=false;//默认不采用异步的方式
		data=param.url+"?"+data;//将数据和URL整合
		dataType=param.dataType?param.dataType:dataType;//判断是否设置了dataType
		async=param.async?param.async:async;//判断是否设置了异步
		ajaxObject.open("GET",data,async);
		ajaxObject.onreadystatechange=function(){
			if (ajaxObject.readyState==4) {
				if(ajaxObject.status==200){
					param.success(dealResponseData(param.dataType,ajaxObject.responseText));
				}else{
					try{
						param.error(ajaxObject.status);
					}catch(e){}
				}
				
			};
		};

		ajaxObject.send(null);

	};
	//POST方式提交数据
	this.post=function(param){
		var dataType="text";//数据传输格式,默认为text
		var data=this.objToStr(param.data);
		var async=false;//默认不采用异步的方式
		dataType=param.dataType?param.dataType:dataType;//判断是否设置了dataType
		async=param.async?param.async:async;//判断是否设置了异步
		ajaxObject.open("POST",param.url,async);
		ajaxObject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		ajaxObject.onreadystatechange=function(){
			if (ajaxObject.readyState==4) {
				if(ajaxObject.status==200){
					param.success(dealResponseData(param.dataType,ajaxObject.responseText));
				}else{
					try{
						param.error(ajaxObject.status);
					}catch(e){}
				}
				
			};
		};

		ajaxObject.send(data);
	};

	//将url参数转换成Object对象
	this.strToObj=function(str,type){
		var urlArr=str.split("&");
		var strLength=urlArr.length;
		var jsonStr="";
		type=type?type:"OBJ";
		for (var i = 0; i < strLength; i++) {
			if (i==(strLength-1)) {
				jsonStr+=urlArr[i].split("=")[0]+":\""+urlArr[i].split("=")[1]+"\"";
			}else{
				jsonStr+=urlArr[i].split("=")[0]+":\""+urlArr[i].split("=")[1]+"\",";
			};
		};
		jsonStr="{"+jsonStr+"}";
		if (type=="OBJ") {
			return eval('object='+jsonStr);
		}else{
			return jsonStr;
		};
	};
	//将Object对象转换成url字符串
	this.objToStr=function(obj){
		var data="";
		var i=1;
		for(p in obj){
			if (i==1) {
				data=p+"="+obj[p];
			}else{
				data+="&"+p+"="+obj[p];
			};
			i++;
		}
		return data;
	};
	this.getCookie=function(c_name){
		if(document.cookie.length>0){
   			c_start=document.cookie.indexOf(c_name + "=");
   			if(c_start!=-1){
    			c_start=c_start + c_name.length+1;
     			c_end=document.cookie.indexOf(";",c_start);
     			if(c_end==-1) c_end=document.cookie.length;
     			return unescape(document.cookie.substring(c_start,c_end));
   			}
		}
		return ""
	}
	this.setCookie=function(name,value,expires){
		var time=expires?expires:"30d";//判断是否设置了值，没有设置则使用默认值
		var unit=time.substring(time.length-1);//取出单位
		var exp = new Date();
		time=parseInt(time);
		switch(unit){
			case "d":
				exp.setTime(exp.getTime() + time*24*60*60*1000);
				break;
			case "h":
				exp.setTime(exp.getTime() + time*60*60*1000);
				break;
			case "m":
				exp.setTime(exp.getTime() + time*60*1000);
				break;
			case "s":
				exp.setTime(exp.getTime() + time*1000);
				break;
			default:
				exp.setTime(exp.getTime() + time*24*60*60*1000);
		}
 		document.cookie = name + "="+ escape (value) + ";expires=" + exp.toUTCString();
	}

	//返回XMLHttpRequest对象
	this.getPureAjaxObject=function(){return ajaxObject;};

}
